local EffectLogic = require("scene.EffectLogic")
local Msg = require("core.Msg")
local Obj = require("core.Obj")
local Timer = require("core.Timer")

function init()
	if not list then
		list = {}
		listlen = 0
		listcap = 1000
		for i = 1, listcap do
			local obj = {die = 1, pos = i}
			list[i] = obj
			Obj.create(obj, Obj.TYPE_TOMB)
		end
	end
end

local last_del_pos = 0

function create()
	if listlen < listcap then
		listlen = listlen + 1
		local obj = list[listlen]
		obj.uid = Timer.now
		return obj
	end
	last_del_pos = last_del_pos + 1
	if listlen < last_del_pos then
		last_del_pos = 1
	end
	local obj = list[last_del_pos]
	Obj.leaveScene(obj)
	obj.uid = Timer.now
	return obj
end

function destroy(obj)
	if listlen < obj.pos then
		assert()
	end
	if list[obj.pos] ~= obj then
		assert()
	end
	local pos = obj.pos
	local lastobj = list[listlen]
	obj.pos, lastobj.pos = lastobj.pos, obj.pos
	list[pos], list[listlen] = list[listlen], list[pos]
	listlen = listlen - 1
	Obj.leaveScene(obj)
end

function time()
	local now = Timer.now
	for i = listlen, 1, -1 do
		local obj = list[i]
		if 300 * 1000 < now - obj.uid then
			destroy(obj)
		end
	end
end

local body_id = {197, 198, 199}
local body_id_len = #body_id
local effectConfig = {}
for k, v in ipairs(body_id) do
	effectConfig[k] = EffectLogic.getConfig(v)
end
function getObjAdd(obj)
	local mm = Msg.gc.GC_ADD_DISPLAY
	mm.obj_id = obj.id
	local pos = obj.id % body_id_len + 1
	mm.body_id = body_id[pos]
	mm.x = obj.x
	mm.y = obj.y
	mm.effectConfig = effectConfig[pos]
	mm.name = ""
	return mm
end
